//      Copyright (c) Microsoft Corporation. All rights reserved.
#include <windows.h>
#include "dpwssideshow.h"

#define COUNTOF(a)  (sizeof(a)/sizeof(a[0]))

CFunctionDiscoveryListener::CFunctionDiscoveryListener(const wstring& deviceID, 
                               CDeviceProxyProvider* pDeviceProxyProvider)
{
    m_deviceID = deviceID;
    m_pDeviceProxyProvider = pDeviceProxyProvider;
    m_cRef = 1;
}

CFunctionDiscoveryListener::~CFunctionDiscoveryListener()
{
}

//------------------------------------------------------------------------------
// QueryInterface
//      IUnknown implementation
//------------------------------------------------------------------------------
HRESULT CFunctionDiscoveryListener::QueryInterface(
    REFIID riid, 
    void** ppvObject)
{
    HRESULT hr = S_OK;

    if( NULL == ppvObject )
    {
        return E_INVALIDARG;
    }

    *ppvObject = NULL;

    if( __uuidof(IFunctionDiscoveryNotification) == riid )
    {
        *ppvObject = static_cast<IFunctionDiscoveryNotification*>(this);
        AddRef();
    }
    else if( __uuidof(IUnknown) == riid )
    {
        *ppvObject = static_cast<IUnknown*>(this);
        AddRef();
    }
    else
    {
        hr = E_NOINTERFACE;
    }

    return hr;
}

//------------------------------------------------------------------------------
// AddRef
//      IUnknown implementation
//------------------------------------------------------------------------------
ULONG CFunctionDiscoveryListener::AddRef()
{
    return InterlockedIncrement( &m_cRef );
}

//------------------------------------------------------------------------------
// Release
//      IUnknown implementation
//------------------------------------------------------------------------------
ULONG CFunctionDiscoveryListener::Release()
{
    LONG cRef = InterlockedDecrement( &m_cRef );

    if( 0 == cRef )
    {
        delete this;
    }

    return cRef;
}

//------------------------------------------------------------------------------
// Release
//      IFunctionDiscoveryNotification implementation
//------------------------------------------------------------------------------
HRESULT CFunctionDiscoveryListener::OnUpdate( 
    QueryUpdateAction enumQueryUpdateAction,
    FDQUERYCONTEXT fdqcQueryContext,
    IFunctionInstance *pFunInst)
{
    if (QUA_ADD != enumQueryUpdateAction)
        return S_OK;

    wstring deviceID;
    GetFunctionInstancePNPXID(pFunInst, deviceID);
    if (deviceID == m_deviceID)
        m_pDeviceProxyProvider->UpdateProxy(deviceID);
    return S_OK;
}

//------------------------------------------------------------------------------
// OnError
//      IFunctionDiscoveryNotification implementation
//------------------------------------------------------------------------------
HRESULT CFunctionDiscoveryListener::OnError( 
                                  /* [in] */ HRESULT hr,
                                  /* [in] */ FDQUERYCONTEXT fdqcQueryContext,
                                  /* [string][in] */ __RPC__in const WCHAR *pszProvider)
{
    return S_OK;
}

//------------------------------------------------------------------------------
// OnEvent
//      IFunctionDiscoveryNotification implementation
//------------------------------------------------------------------------------
HRESULT CFunctionDiscoveryListener::OnEvent( 
    DWORD dwEventID,
    FDQUERYCONTEXT fdqcQueryContext,
    const WCHAR *pszProvider)
{
    return S_OK;
}